package com.inmarry.action;

import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Date;

import org.apache.struts.action.*;
import org.apache.struts.util.*;
import org.apache.struts.validator.*;

import com.inmarry.model.*;
import com.inmarry.form.*;
import com.inmarry.util.*;

import com.ibatis.sqlmap.client.SqlMapClient;


public final class Register extends Action {
    public ActionForward execute(
            ActionMapping mapping,
            ActionForm form,
            HttpServletRequest req,
            HttpServletResponse res ) throws Exception {

        HttpSession session = req.getSession(true);

        //建立错误消息文件
        ActionMessages errors = new ActionMessages();

        //用户的输入已经不需要再进行检测了，
        //因为我们已经在表单里进行检测过了。
        //所以到这里我们只需要进行唯一性检测即可。
        //用户输入的用户名
        String userName = (String)((DynaValidatorForm)form).get("userName");
        //用户输入的密码
        String password = (String)((DynaValidatorForm)form).get("password");
        //用户输入的邮件
        String email = (String)((DynaValidatorForm)form).get("email");


        //用户输入的验证码
        String verifyCodeInForm = (String)((DynaValidatorForm)form).get("verifyCode");
        String verifyCodeInSession = (String)session.getAttribute("verifycode");

        if ( !verifyCodeInForm.equalsIgnoreCase(verifyCodeInSession) ) {
            errors.add("verifyCode", new ActionMessage("register.verifycode.not.currect"));
        } 
    
        //get SQLMap;
        SqlMapClient sqlMap = DBConnection.getSqlMapInstance();

        //检查相应的用户名是否存在
        UserBase user = (UserBase)sqlMap.queryForObject("getUserByName", userName);
        if ( user != null ) {
            errors.add("userName", new ActionMessage("register.username.exists", userName));
        }

        user = (UserBase)sqlMap.queryForObject("getUserByEmail", email);
        if ( user != null ) {
            errors.add("email", new ActionMessage("register.email.exists", email));
        }

        //判断ActionMessages对象是否为空，如果为空
        //责显示错误信息，并返回用户注册表单
        if ( !errors.isEmpty() ) {
            saveErrors(req,errors);
            return new ActionForward(mapping.getInput());
        }


        //加密用户密码
        String securityPassword = MD5.md5encoder(password);
        String activeCode = MD5.md5encoder(securityPassword + userName);

        //注册一个新用户
        UserBase newUser = new UserBase();
        newUser.setUserName(userName);
        newUser.setPassword(securityPassword);
        newUser.setEamil(email);
        newUser.setRegisterDate(new Date());
        newUser.setActiveCode(activeCode);
        
        sqlMap.insert("newUser", newUser);




        //给用户发送邮件
        try {
            SendEmail sendEmail = new SendEmail();
            sendEmail.setTo(email);
            sendEmail.sendRegisterEmail(activeCode);

        } catch ( Exception ex ) {
            System.err.println(ex);
            ex.printStackTrace();
            System.err.println("邮件发送失败，请检查邮件帐户");
        }

        //判断用户邮件的供应商。从而为显示邮件的商标在页面上做准备
        String[] emailArray = email.split("@");

        String emailIcon = "";
        String emailUrl = "";

        if ( emailArray[1].equalsIgnoreCase("21cn.com") ) {
            emailIcon = "21cn.gif";
            emailUrl = "http://mail.21cn.com/";
        } else if ( emailArray[1].equalsIgnoreCase("126.com") ) {
            emailIcon = "126.gif";
            emailUrl = "http://www.126.com/";
        } else if ( emailArray[1].equalsIgnoreCase("163.com") ) {
            emailIcon = "163.gif";
            emailUrl = "http://mail.163.com/";
        } else if ( emailArray[1].equalsIgnoreCase("188.com") ) {
            emailIcon = "188.gif";
            emailUrl = "http://www.188.com/";
        } else if ( emailArray[1].equalsIgnoreCase("vip.163.com") ) {
            emailIcon = "vip163.gif";
            emailUrl = "http://vip.163.com/";
        } else if ( emailArray[1].equalsIgnoreCase("netease.com")) {
            emailIcon = "yeah.gif";
            emailUrl = "http://mail.netease.com/";
        } else if ( emailArray[1].equalsIgnoreCase("yeah.net") ) {
            emailIcon = "yeah.gif";
            emailUrl = "http://www.yeah.net/";
        } else if ( emailArray[1].equalsIgnoreCase("gmail.com") ) {
            emailIcon = "gmail.gif";
            emailUrl = "http://mail.google.com/gmail";
        } else if ( emailArray[1].equalsIgnoreCase("hotmail.com") 
                || emailArray[1].equalsIgnoreCase("msn.com") ) {
            emailIcon = "msn.gif";
            emailUrl = "http://mail.live.com";
        } else if ( emailArray[1].equalsIgnoreCase("qq.com") 
                || emailArray[1].equalsIgnoreCase("vip.qq.com") 
                || emailArray[1].equalsIgnoreCase("foxmail.com") ) {
            emailIcon = "qq.gif";
            emailUrl = "http://mail.qq.com/";
        } else if ( emailArray[1].equalsIgnoreCase("sina.com")
                || emailArray[1].equalsIgnoreCase("vip.sina.com") ) {
            emailIcon = "sina.gif";
            emailUrl = "http://mail.sina.com.cn";
        } else if ( emailArray[1].equalsIgnoreCase("sohu.com")
                || emailArray[1].equalsIgnoreCase("vip.sohu.com") ) {
            emailIcon = "sohu.gif";
            emailUrl = "http://mail.sohu.com/";
        } else if ( emailArray[1].equalsIgnoreCase("tom.com") ) {
            emailIcon = "tom.gif";
            emailUrl = "http://mail.tom.com/";
        } else if ( emailArray[1].equalsIgnoreCase("vip.tom.com") ) {
            emailIcon = "viptom.gif";
            emailUrl = "http://vip.tom.com/";
        } else if ( emailArray[1].equalsIgnoreCase("yahoo.com") ) {
            emailIcon = "yahoo.gif";
            emailUrl = "http://mail.yahoo.com";
        } else if ( emailArray[1].equalsIgnoreCase("yahoo.com.cn") ) {
            emailIcon = "yahoocn.gif";
            emailUrl = "http://mail.cn.yahoo.com/";
        } else if ( emailArray[1].equalsIgnoreCase("sogou.com") ) {
            emailIcon = "sogou.gif";
            emailUrl = "http://mail.sogou.com/";
        } else if ( emailArray[1].equalsIgnoreCase("eyou.com")
                || emailArray[1].equalsIgnoreCase("vip.eyou.com") ) {
            emailIcon = "eyou.gif";
            emailUrl = "http://www.eyou.com/";
        } else {
            emailIcon = "mail.gif";
            emailUrl = "";
        }

        req.setAttribute("emailIcon", emailIcon);
        req.setAttribute("emailUrl", emailUrl);
        session.removeAttribute("verifycode");


        return mapping.findForward("successRegister");
    }
}

